package com.dpwgc.provider.controller;

import com.alibaba.fastjson.JSON;
import com.dpwgc.provider.model.User;
import com.dpwgc.provider.service.UserService;
import com.dpwgc.provider.utils.ResultUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping(value = "/User")
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 通过openid查询用户
     * @param params
     * @return
     */
    @RequestMapping(value = "/queryUserByOpenid",method = RequestMethod.POST)
    public ResultUtil<User> queryUserByOpenid(@RequestParam Map<String, String> params){
        String UserOpenid = params.get("UserOpenid");
        return userService.queryUserByOpenidService(UserOpenid);
    }

    /**
     * 获取用户openid
     * @param params
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping(value = "/getUserOpenid",method = RequestMethod.POST)
    public ResultUtil<Object> getUserOpenid(@RequestParam Map<String,String> params) throws JsonProcessingException {
        String Code = params.get("code");
        return userService.getUserOpenidService(Code);
    }

    /**
     * 手机号+密码登录
     *
     * @param phoneNum
     * @param password
     * @return
     */
    @RequestMapping(value = "/phoneNumLogin",method = RequestMethod.POST)
    public ResultUtil<Object> phoneNumLogin(@RequestParam("phoneNum") String phoneNum,
                                            @RequestParam("password") String password) {

        return userService.phoneNumLoginService(phoneNum, password);
    }

    /**
     * 插入（注册）用户
     * @param params
     * @return
     */
    @RequestMapping(value = "/insertUser",method = RequestMethod.POST)
    public ResultUtil<Object> insertUser(@RequestParam Map<String,String> params){

        User user = JSON.parseObject(JSON.toJSONString(params), User.class);
        return userService.insertUserService(user);
    }

    /**
     * 更新用户信息
     * @param params
     * @return
     */
    @RequestMapping(value = "/updateUserInfoByUserUuid",method = RequestMethod.POST)
    public ResultUtil<Object> updateUserInfoByUserUuid(@RequestParam Map<String,String> params){

        User user = JSON.parseObject(JSON.toJSONString(params), User.class);
        return userService.updateUserInfoByUserUuidService(user);
    }

    /**
     * 通过userUuid获取用户信息
     * @param userUuid
     * @return
     */
    @RequestMapping(value = "/queryUserByUuid",method = RequestMethod.POST)
    public ResultUtil<Object> queryUserByUuid(@RequestParam("userUuid") String userUuid){
        return userService.queryUserByUuidService(userUuid);

    }

}
